let detailName;
let detailYear;
$(function() {
	const bgColors = ['#E8F7FF', '#E8FFFB', '#E8F3FF', '#FFFCE8', '#FFF7E8', '#F5E8FF'];
	const txtColors = ['#3491FA', '#14C9C9', '#165DFF', '#F7BA1E', '#FF7D00', '#722ED1'];
	for (let i = 0; i < $('.abstract ul li h6').length; i++) {
		const index = Math.floor(Math.random() * bgColors.length)
		$('.abstract ul li h6').eq(i).css('background-color', bgColors[index])
		$('.abstract ul li h6').eq(i).css('color', txtColors[index])
	}
	$('.welcome h2').text('👏 欢迎回来，' + localStorage.getItem('user'))


	$('.abstract').on('click', '.projects ul li h3', function() {
		window.location.href = './projectDetail.html'
	})
	renderQuarterSubject()


	$('.table-warp').on('click', '.bootstrap-table .fixed-table-container .table p', function() {
		detailName = $(this).context.dataset.name;
		detailYear = $(this).context.dataset.year;
		$('.mask .mask-content .select.type h4').text(detailName)
		$('.mask .mask-content .select.year h4').text(detailYear)
		$('.mask').show()
		renderOverYears()
		renderMonth()
	})
	$('.mask .mask-content .select').click(function(event) {
		event.stopPropagation();
		$(this).find('.items').toggle()
	})
	$('.mask .mask-content .select ').on('click', '.items p', function() {
		$(this).parents('.select').find('h4').text($(this).text())
		if ($(this).parents('.select').hasClass('type')) {
			detailName = $(this).text()
		} else {
			detailYear = $(this).text()
		}
		renderOverYears($(this).text())
		// $('.mask .mask-content .select .items').hide()
	})
	$(document).click(function() {
		$('.mask .mask-content .select .items').hide()
	})
	$('.mask .mask-content .title>img').click(function() {
		$('.mask').hide()
	})
})

function renderQuarterSubject() {
	// $.get(`${API_URL}getdata`, {
	// 	username: getUsername,
	// 	status: 2,
	// 	chartPage: 80803,
	// 	xm01: '',
	// 	xm02: '2022',
	// 	chartNo: 4
	// }, function(res) {
	// 	const resp = eval("(" + res + ")").items[0].charts[0];
	// 	const name = resp.data.length > 0 ? resp.data[0].key02.split(',') : []
	// 	getTableData(resp.data)
	// })
	const resp = {"chart_no":"4","chart_name":"支出-季度科目费用{参数：xm01-任务编号；xm02-年份；返回值：key01-季度；key02-科目名称；key03-金额}","key_count":"3","data":[{"key01":"一季度","key02":"事务费,固定资产折旧费,外协费,工资及劳务费,拨入科研费,材料费,燃料动力费,管理费","key03":"0,0,0,0,-2419.88,-29.47,0,0"},{"key01":"二季度","key02":"专用费,事务费,固定资产折旧费,外协费,拨入科研费,材料费,燃料动力费,管理费","key03":"310.64,.96,0,2020.56,198052.89,31.02,0,49.28"},{"key01":"三季度","key02":"专用费,事务费,固定资产折旧费,外协费,拨入科研费,材料费,燃料动力费,管理费","key03":"651.3,1.86,0,2204.51,507505.35,3259.9,0,63.98"},{"key01":"四季度","key02":"专用费,事务费,固定资产折旧费,外协费,工资及劳务费,拨入科研费,材料费,燃料动力费,管理费","key03":"340.87,1.5,2305.32,10324.11,9423.52,2764702.57,5010.61,1041.56,5227.64"}]};
	const name = resp.data.length > 0 ? resp.data[0].key02.split(',') : []
	getTableData(resp.data)
}

function getTableData(data) {
	if (data.length === 0 || !data[0]) {
		renderTable1([], [])
		return
	}
	const firstColumns = data[0].key02.split(',')
	let newData = firstColumns.map(it => ({
		key01: it,
		key02: 0
	}))
	for (let i = 0; i < data.length; i++) {
		for (let j = 0; j < newData.length; j++) {
			newData[j]['key02'] += +data[i].key03.split(',')[j]
			newData[j]['key0' + (i + 3)] = data[i].key03.split(',')[j]
		}
	}
	// const thead = data.map(it => it.key01)
	const thead = ['2020', '2021', '2022', '2023']
	renderTable1(newData, thead)
}

function renderTable1(data, thead) {
	const columns = [];
	columns[0] = thead.map((it, i) => ({
		field: 'key0' + (i + 3),
		title: it,
		align: 'right',
		valign: 'middle',
		width: 100,
		formatter: function(value, data) {
			return `<p data-name=${data.key01} data-year=${it}>${value}</p>`
		}
	}))
	columns[0].unshift({
		field: 'key01',
		title: '类别',
		align: 'left',
		width: 100,
		valign: 'middle',
		formatter: blodText
	})
	renderTable('table1', data, columns, false)
}
const rateparams = {
	username: getUsername,
	xm01: fullTaskNumber,
	status: 2,
	chartPage: 80802
}

function renderOverYears() {
	// $.get(`${API_URL}getdata`, {
	// 	...rateparams,
	// 	xm01: fullTaskNumber,
	// 	chartNo: 2
	// }, function(res) {
	// 	const resp = eval("(" + res + ")").items[0].charts[0];
	// 	const name = [detailName];
	// 	renderLineChart('chart2', resp, name, '年')
	// })
	const resp = {"chart_no":"2","chart_name":"收入-历年财务到款{传参：xm01任务编号；返回值：key01-年份；key02-金额}","key_count":"2","data":[{"key01":"2022","key02":"389.05"},{"key01":"2019","key02":"125.63"},{"key01":"2020","key02":"174.8"},{"key01":"2018","key02":"68.05"},{"key01":"2021","key02":"279.7"}]};
	const name = [detailName];
	renderLineChart('chart2', resp, name, '年')
}

function renderMonth() {
	// $.get(`${API_URL}getdata`, {
	// 	...rateparams,
	// 	xm01:fullTaskNumber,
	// 	chartNo:3
	// }, function(res) {
	// 	const resp = eval("(" + res + ")").items[0].charts[0];
	// 	const name = ['年度预算'];
	// 	renderLineChart('chart1', resp, name,'')
	// })
	const resp = {
		data: [{
				key01: "事务费",
				key02: "1389.05"
			},
			{
				key01: "固定资产折旧费",
				key02: "549.05"
			},
			{
				key01: "外协费",
				key02: "309.32"
			},
			{
				key01: "工资及劳务费",
				key02: "529.05"
			},
			{
				key01: "拨入科研费",
				key02: "159.05"
			},
			{
				key01: "材料费",
				key02: "39.05"
			},
			{
				key01: "燃料动力费",
				key02: "879.05"
			},
			{
				key01: "管理费",
				key02: "439.05"
			}
		]

	};
	const name = ['年度预算'];
	renderLineChart('chart1', resp, name, '')
}

function renderLineChart(elId, data, legends, unit) {
	noData(elId, data)
	let colors = ['#165DFF', '#14C9C9', '#F7BA1E']
	const legendData = legends
	let xData = [];
	for (let i = 0; i < data.data.length; i++) {
		xData.push(data.data[i].key01);
	}
	const seriesData = [{
		name: '',
		type: "line",
		symbol: 'none',
		smooth: true,
		areaStyle: {
			color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
				offset: 0,
				color: 'rgba(20, 201, 201, 0.20)' // 0% 处的颜色
			}, {
				offset: 1,
				color: 'rgba(29,63,120,0)' // 100% 处的颜色
			}], false),
		},
		itemStyle: {
			color: colors[1],
		},
		smooth: true,
		data: [],
	}];
	for (const item of data.data) {
		seriesData[0].data.push(item['key02'])
	}
	have(elId) ? lineChartkzks(elId, seriesData, legendData, xData, unit, '万元', '30') : "";
}
